import { createRouter , createWebHistory} from "vue-router";
import globalVariables from "@/utils/globalVariables.js";

const routes =[
    {
        path: '/',
        redirect: '/login',
    },
    {
        name: '首 页',
        path: '/home',
        component: () => import('../views/home/index.vue')
    },
    {
        name: '登录页',
        path: '/login',
        component: () => import('../views/login/index.vue')
    },
]

// 不是通过new来创建， 通过createRouter 方法创建  使用的模式不是通过mode来定义
const router = createRouter({
    routes,
    history: createWebHistory(),
})

// 判断账号是否登录
router.beforeEach((to, from, next) => {
    if (to.path !== '/login' && !globalVariables.isLoggedIn) {
        // 当账号没有登录 则跳转至登录页面
        next('/login');
    } else {
        // 当账号登录 则进行下一步
        next();
    }
});


export default router
